package com.lamda.zhekou.controller;

import com.lamda.zhekou.exception.ZheKouException;
import com.lamda.zhekou.manager.ServerHostManager;
import com.lamda.zhekou.manager.UserManager;
import com.lamda.zhekou.manager.VPSRecordManager;
import com.lamda.zhekou.model.ServerHost;
import com.lamda.zhekou.model.User;
import com.lamda.zhekou.model.UserVps;
import com.lamda.zhekou.model.VPS;
import com.lamda.zhekou.vo.UserVpsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;


/**
 * Created by MJiao on 2/11/15.
 */
@Controller
public class HomeController {

    @Autowired
    private VPSRecordManager vpsRecordManager;
    @Autowired
    private UserManager userManager;
    @Autowired
    private ServerHostManager serverHostManager;

    @RequestMapping(value = "/")
    public String home(HttpServletRequest httpServletRequest, Model model) {
        User user = (User) httpServletRequest.getSession().getAttribute("loginedUser");
//        int vpsRecordCountByUserId = this.vpsRecordManager.getVPSRecordCountByUserId(user.getId());
//        model.addAttribute("vpsRecordCount", vpsRecordCountByUserId);
        model.addAttribute("vpsRecordCount", 0);

        List<UserVps> userVpsList =this.serverHostManager.findUserVpsListByUserId(user.getId());
        List<UserVpsVo> userVpsVoList = UserVpsVo.fromUserVpsList(userVpsList);
        model.addAttribute("userVpsVoList", userVpsVoList);
        return "index";
    }



    @RequestMapping(value = "/initData")
    @ResponseBody
    public String home() {
        StringBuilder sb = new StringBuilder();
        try {
            this.initUserData();
            sb.append("Init UserData successfully\n");
        } catch (ZheKouException e) {
            e.printStackTrace();
            sb.append("Init UserData failly").append(e.getMessage()).append("-------------\n");
        }
//        try {
//            this.initHostData();
//            sb.append("初始化Host数据成功\n");
//        } catch (ZheKouException e) {
//            e.printStackTrace();
//            sb.append("初始化Hos数据失败").append(e.getMessage()).append("-------------\n");
//        }
//     try {
//            this.initVps();
//         sb.append("Init vps data successfully\n");
//        } catch (ZheKouException e) {
//            e.printStackTrace();
//            sb.append("Init vps data fail!").append(e.getMessage()).append("-------------\n");
//        }
        return sb.toString();
    }

    private void initVps() throws ZheKouException {
       ServerHost serverHost =  this.serverHostManager.fgetServerHostByIp("192.168.1.1");
        for (int idx = 0; idx < 10; idx++){
            VPS vps = new VPS();
            vps.setVmName("vps_idx_1_" + idx);
            vps.setVmId(UUID.randomUUID().toString());
            serverHost.getVpsSet().add(vps);
        }
        this.serverHostManager.update(serverHost);
        ServerHost serverHost2 =  this.serverHostManager.fgetServerHostByIp("192.168.1.2");
        for (int idx = 0; idx < 10; idx++){
            VPS vps = new VPS();
            vps.setVmName("vps_idx_2_" + idx);
            vps.setVmId(UUID.randomUUID().toString());
            serverHost2.getVpsSet().add(vps);
        }
        this.serverHostManager.update(serverHost2);

    }
    private void initHostData() throws ZheKouException {
        for (int idx = 0; idx < 50; idx++) {
            ServerHost serverHost = new ServerHost();
            serverHost.setHostname("hostname_" + idx);
            serverHost.setIp("192.168.1." + (idx + 1));


            this.serverHostManager.registerServerHost(serverHost);

        }
    }

    private void initUserData() throws ZheKouException {
        for (int idx = 0; idx < 50; idx++) {
            User user = new User();
            user.setNickname("nickName_" + idx);
            user.setTelephone(String.valueOf(10000000 + idx * 2));
            user.setQq(String.valueOf(2000000 + idx * 101));
            user.setEmail("email_" + idx + "_@qq.com");
            user.setPassword("123456");
            user.setInviteCode("JiaoMing");
            user.setRegisterTime(new Timestamp(System.currentTimeMillis()));
            this.userManager.register(user);
        }
    }
}
